package com.example.concurrentprogramming8016.future.futuretask;



import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author liyanhao
 * @Date 2023-01-08 15:24
 */
public class FutureTaskDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        DemoTask demoTask = new DemoTask();
        //构建futureTask
        FutureTask<Integer> integerFutureTask = new FutureTask<>(demoTask);
        new Thread(integerFutureTask).start();
        System.out.println("futureTask的计算结果:" + integerFutureTask.get());

    }

    static class DemoTask implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            System.out.println("子线程正在计算");
            int sum = 0;
            for (int i = 0; i < 100; i++) {
                sum += i;
            }
            return sum;
        }

    }
}
